# -*- coding: utf-8 -*-
#exception 异常
import exceptions
dir(exceptions) #查看异常所有方法

#try except 捕捉异常 except可以跟多个可能引发的异常
def demo1():
	num1=input('first:')
	num2=input('section:')
	try:
		result=num1/num2
		print result
	except ZeroDivisionError:
		print '第二个数不能为0'.decode('utf-8')
	except TypeError:
		print '不是一个数字'.decode('utf-8')
	
#用一个块捕捉两个异常
def demo2():
	num1=input('first:')
	num2=input('section:')
	try:
		result=num1/num2
		print result
	except (ZeroDivisionError,TypeError):
		print '您输入有误'.decode('utf-8')
		
def demo3():
	num1=input('first:')
	num2=input('section:')
	try:
		result=num1/num2
		print result
	except (ZeroDivisionError,TypeError),e:
		print e
		
#真正的全部捉
def demo4():
	num1=input('first:')
	num2=input('section:')
	try:
		result=num1/num2
		print result
	except:
		print '您输入有误'.decode('utf-8')

#万事大吉  
def demo5():   
	while True:  #这段代码如果输入有误的话一直会让用户输入直到正确为止
		try:
			num1=input('first:')
			num2=input('section:')
			result=num1/num2
			print result
		except :
			print '您输入有误'.decode('utf-8')	
		else:
			break
#finally 无论发生不发生异常都会执行的代码
def demo6():
	x=None
	try:
		x=10/0  #由于ZeroError  x永远不会被赋值,这样就会导致finally子句中使用del他的时候引发异常
	finally:
		print 'clean up..'
		del x

def demo7():
	try:
		x=10/0
		print x
	except ZeroDivisionError:   #(except NameError:)
		print 'name is wrong'
	else:
		print 'else'
	finally:
		print 'clean up'
		
		
		
		
		
		
		
		
		




